//###############
//FILE : RemoveFileRequest.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex3 2011
//DESCRIPTION:
//See below
//###############

package oop.ex3.nameserver;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import oop.ex3.protocol.sessions.NameRemoveFileSession;

/**
 * This handles the REMOVEFILE request as specified in the assignment.
 * @author daniel
 */
public class RemoveFileRequest extends FileManagerSpecificRequest{
    private NameRemoveFileSession _removeSession;

    /**
     * Create a new request initialized with a NS database which is passed to the super class.
     * The opening session is a NameRemoveFileSession.
     * @param nsDb The nameserver database.
     */
    public RemoveFileRequest(NameServerDatabase nsDb) {
        super(nsDb, new NameRemoveFileSession());
        _removeSession = (NameRemoveFileSession)_openingSession;
    }

    @Override
    public boolean serveRequest(DataInputStream inputStream, DataOutputStream outputStream) {
        //System.out.println("serving remove request");
        //first run the remove session
        if (!_removeSession.executeSession(inputStream, outputStream)) {
            return false;
        }

        
        //if we didn't find the end  then there was an error
        if (!_endSession.executeSession(inputStream, outputStream)) {
            return false;
        }

        String fileName = _removeSession.getFileName();
        _nameServerDb.removeFileFromFileManager(_fileManager, fileName);

        return true;
    }

    @Override
    public NameServerRequest getCopy() {
        RemoveFileRequest copy = new RemoveFileRequest(_nameServerDb);
        return copy;
    }

}
